package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/shop")
@Slf4j
@Api(tags = "店铺操作接口")
public class ShopController {
    // 注入redis模板对象
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 设置营业状态
     *
     * @return
     */
    @ApiOperation("设置营业状态")
    @PutMapping("/{status}")
    public Result setStatus(@PathVariable Integer status) {
        log.info("设置店铺的状态为:{}", status == 1 ? "营业中" : "打烊中");
        // 使用redis数据库来存储数据
        // 调用redis模板对象获取操作对象
        ValueOperations opsForValue = redisTemplate.opsForValue();
        opsForValue.set(MessageConstant.KEY, status);
        return Result.success();
    }

    /**
     * 获取营业状态
     *
     * @return
     */
    @GetMapping("/status")
    @ApiOperation("获取营业状态")
    public Result getStatus() {
        // 注入使用redis模板对象获取reids操作对象
        ValueOperations opsForValue = redisTemplate.opsForValue();
        // 把设置的营业状态值取出来
        Object status = opsForValue.get(MessageConstant.KEY);
        return Result.success(status == null ? 0 : status);
    }

}
